'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
exports.LayerManager = void 0
var LayerManager = /** @class */ (function () {
  function LayerManager(id, map) {
    this._id = id
    this._map = map
    this._layers = []
    this._eventListener = new Map()
  }
  Object.defineProperty(LayerManager.prototype, 'layers', {
    get: function () {
      return this._layers
    },
    enumerable: false,
    configurable: true,
  })
  LayerManager.prototype.on = function (type, callback) {
    var _a
    if (this._eventListener.has(type)) {
      ;(_a = this._eventListener.get(type)) === null || _a === void 0
        ? void 0
        : _a.push(callback)
    } else {
      this._eventListener.set(type, [callback])
    }
  }
  LayerManager.prototype.off = function (type, callback) {
    if (this._eventListener.has(type)) {
      if (callback) {
        var functionList = this._eventListener.get(type)
        if (functionList) {
          var index = functionList.findIndex(function (item) {
            return item === callback
          })
          if (index !== -1) {
            functionList.splice(index, 1)
          }
        }
      } else {
        this._eventListener.delete(type)
      }
    }
  }
  LayerManager.prototype.fire = function (type, params) {
    var _a
    if (this._eventListener.has(type)) {
      ;(_a = this._eventListener.get(type)) === null || _a === void 0
        ? void 0
        : _a.forEach(function (fn) {
            fn(params)
          })
    }
  }
  /**
   * 添加图层
   *
   * @param {string} id 图层id
   *
   * @param {string} name 图层名称
   *
   * @param {boolean} [visible=true] 图层显隐
   *
   * @param {number} [opacity=1] 图层不透明度
   *
   * @param {Layer[]} [layers] 包含子图层
   */
  LayerManager.prototype.addLayer = function (
    id,
    name,
    visible,
    opacity,
    layers
  ) {
    if (visible === void 0) {
      visible = true
    }
    if (opacity === void 0) {
      opacity = 1
    }
    var data = {
      id: id,
      name: name,
      visible: visible,
      opacity: opacity,
      layers: layers,
    }
    this._layers.push(data)
    this.fire('add', data)
  }
  /**
   * 移除图层
   *
   * @param {string} id 图层id
   *
   * @return {boolean} 是否移除成功
   */
  LayerManager.prototype.removeLayer = function (id) {
    var index = this._layers.findIndex(function (layer) {
      return layer.id === id
    })
    if (index === -1) {
      return false
    } else {
      this._layers.splice(index, 1)
      this.fire('remove', id)
      return true
    }
  }
  return LayerManager
})()
exports.LayerManager = LayerManager
